package com.tuanzi.loan.business.repository.settlementsystem.impl;

import com.tuanzi.loan.business.entity.family.ProductInfo;
import com.tuanzi.loan.business.entity.settlementsystem.SettlementProduct;
import com.tuanzi.loan.business.repository.settlementsystem.custom.SettlementProductRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.vo.request.settlementsystem.SettlementProductRequest;
import com.tuanzi.loan.vo.response.settlementsystem.SettlementProductResponse;
import org.apache.commons.lang3.time.DateUtils;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import java.util.Date;
import java.util.List;

import static com.tuanzi.loan.generated.jooq.loan.tables.LoanProductInfo.LOAN_PRODUCT_INFO;
import static com.tuanzi.loan.generated.jooq.loan.tables.LoanSettlementCompany.LOAN_SETTLEMENT_COMPANY;
import static com.tuanzi.loan.generated.jooq.loan.tables.LoanSettlementProduct.LOAN_SETTLEMENT_PRODUCT;

/**
 * @author caiwanlin
 * @create 2018-08-16 16:21
 */
public class SettlementProductRepositoryImpl extends JOOQBaseRepositoryPlus implements SettlementProductRepositoryCustom {
    @Override
    protected TableImpl getTable() {
        return LOAN_SETTLEMENT_PRODUCT;
    }

    @Override
    public Page<SettlementProductResponse> list(SettlementProductRequest searchRequest, Pageable pageable) {
        String productName = searchRequest.getProductName();
        Integer copMode = searchRequest.getCopMode();
        Integer spreadChannel = searchRequest.getSpreadChannel();
        Integer spreadMode = searchRequest.getSpreadMode();
        String creator = searchRequest.getCreator();
        Date startTime = searchRequest.getStartTime();
        Date endTime = searchRequest.getEndTime();
        if (endTime != null){
            endTime = DateUtils.addHours(endTime, 23);
            endTime = DateUtils.addMinutes(endTime,59);
            endTime = DateUtils.addSeconds(endTime,59);
        }
        fields(LOAN_SETTLEMENT_PRODUCT.fields());
        contains(LOAN_SETTLEMENT_PRODUCT.PRODUCT_NAME,productName);
        eq(LOAN_SETTLEMENT_PRODUCT.COP_MODE,copMode);
        eq(LOAN_SETTLEMENT_PRODUCT.SPREAD_CHANNEL,spreadChannel);
        eq(LOAN_SETTLEMENT_PRODUCT.SPREAD_MODE,spreadMode);
        eq(LOAN_SETTLEMENT_PRODUCT.CREATOR,creator);
        between(LOAN_SETTLEMENT_PRODUCT.CREATE_TIME,startTime,endTime);
        orderBy(LOAN_SETTLEMENT_PRODUCT.CREATE_TIME, Sort.Direction.DESC);
        Page<SettlementProductResponse> search = search(pageable, record -> {
            SettlementProductResponse response = new SettlementProductResponse();
            response.setId(record.get(LOAN_SETTLEMENT_PRODUCT.ID));
            response.setProductName(record.get(LOAN_SETTLEMENT_PRODUCT.PRODUCT_NAME));
            response.setSpreadChannel(record.get(LOAN_SETTLEMENT_PRODUCT.SPREAD_CHANNEL));
            response.setSpreadMode(record.get(LOAN_SETTLEMENT_PRODUCT.SPREAD_MODE));
            response.setCopMode(record.get(LOAN_SETTLEMENT_PRODUCT.COP_MODE));
            response.setPriceCpa(record.get(LOAN_SETTLEMENT_PRODUCT.PRICE_CPA));
            response.setPriceCps(record.get(LOAN_SETTLEMENT_PRODUCT.PRICE_CPS));
            response.setSettlementMode(record.get(LOAN_SETTLEMENT_PRODUCT.SETTLEMENT_MODE));
            response.setIncomeType(record.get(LOAN_SETTLEMENT_PRODUCT.INCOME_TYPE));
            response.setActivityIdStr(record.get(LOAN_SETTLEMENT_PRODUCT.ACTIVITY_ID_STR));
            response.setCompanyId(record.get(LOAN_SETTLEMENT_PRODUCT.COMPANY_ID));
            response.setRemark(record.get(LOAN_SETTLEMENT_COMPANY.REMARK));
            response.setCreator(record.get(LOAN_SETTLEMENT_COMPANY.CREATOR));
            response.setCreateTime(record.get(LOAN_SETTLEMENT_COMPANY.CREATE_TIME));
            response.setUpdator(record.get(LOAN_SETTLEMENT_COMPANY.UPDATOR));
            response.setUpdateTime(record.get(LOAN_SETTLEMENT_COMPANY.UPDATE_TIME));
            return response;
        });
        return search;
    }

    @Override
    public List<ProductInfo> getActivityList() {
        List<ProductInfo> productInfoList = dslContext.select(LOAN_PRODUCT_INFO.ID, LOAN_PRODUCT_INFO.NAME)
                .from(LOAN_PRODUCT_INFO).fetchInto(ProductInfo.class);
        return productInfoList;
    }

    @Override
    public List<SettlementProduct> getProductList() {
        fields(LOAN_SETTLEMENT_PRODUCT.ID,LOAN_SETTLEMENT_PRODUCT.PRODUCT_NAME);
        List<SettlementProduct> search = search(SettlementProduct.class);
        return search;
    }
}
